package com.lhb.user.service.impl;



import com.lhb.user.configure.JwtProperties;
import com.lhb.user.constant.Sender;
import com.lhb.user.mapper.UserMapper;
import com.lhb.user.model.*;
import com.lhb.user.service.AuthService;
import com.lhb.user.service.EmailService;
import com.lhb.user.utils.AESEncryptor;
import com.lhb.user.utils.CodeUtil;
import com.lhb.user.utils.JwtTool;
import com.lhb.user.utils.RandomGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author 86131
 * @version 1.0
 * @description 认证服务接口实现
 * @date 2025/4/24 下午3:52
 */
@Slf4j
@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    EmailService emailService;
    @Autowired
    CodeUtil codeUtils;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    JwtTool jwtTool;
    @Autowired
    JwtProperties jwtProperties;

    @Transactional
    @Override
    public void ResetPassword(ResetPasswordRequest resetPasswordRequest) {
        if(!codeUtils.same(resetPasswordRequest.getEmail(), Sender.EmailVerificationCode,resetPasswordRequest.getCode()))
        {
            throw new RuntimeException("验证码过期，请重新获取验证码");
        }
        String newPassword = RandomGenerator.generateRandomPassword();
        try {
            userMapper.updateUserByEmail(AESEncryptor.encrypt(resetPasswordRequest.getEmail()) , AESEncryptor.encrypt(newPassword));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        rabbitTemplate.convertAndSend("email.direct","resetPassword",new SendNewPassswordModel(resetPasswordRequest.getEmail(),newPassword));

    }
    @Transactional
    @Override
    public void register(RegisterModel registerModel) {
        if(!codeUtils.same(registerModel.getEmail(), Sender.EmailVerificationCode,registerModel.getCode()))
        {
            throw new RuntimeException("验证码过期，请重新获取验证码");
        }
        try {
            String username = AESEncryptor.encrypt(registerModel.getUsername());
            String email = AESEncryptor.encrypt(registerModel.getEmail());
            String password =AESEncryptor.encrypt(registerModel.getPassword());
            userMapper.register(new User(username,email,password));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        rabbitTemplate.convertAndSend("email.direct","register",registerModel.getEmail());
    }

    @Override
    public LoginVO login(LoginModel loginModel) throws Exception {

        String email = AESEncryptor.encrypt(loginModel.getEmail()) ;
        String password= AESEncryptor.encrypt(loginModel.getPassword());

        LoginVO loginVO = userMapper.login(email,password);
        if(loginVO ==null)
        {
            throw new RuntimeException("用户名或密码错误,请重试");
        }
        String token = jwtTool.createToken(loginVO.getId(), jwtProperties.getTokenTTL());
        return LoginVO.builder()
                .id(loginVO.getId())
                .username(AESEncryptor.decrypt(loginVO.getUsername()))
                .email(loginModel.getEmail())
                .password(loginModel.getPassword())
                .token(token)
                .build();


    }


}
